/*
 * @(#)MainFrame.java        0.1 09/04/29
 * @version 	0.1 29 Apr 2009
 * @author 	Tran Xuan Thanh
 * 
 * Giao dien chinh cua chuong trinh: co cac Menu, Toolbar va cho phep nguoi dung tao/xoa/chinh sua
 * cac Class va cac quan he
 */


package GUI;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import Data.*;
import XML_IO.*;
import Generate.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;


public class MainFrame extends JFrame implements ActionListener, ClipboardOwner {
	ResourceBundle lang;
	Options options;
	Log log;
	File file;
	Classes classes;
	Lines lines;
	
	static boolean isSaved = true;
	static boolean isLogged = true;
	
	static DesktopPane desktopPane;
	ClassFrame lastActivedClassFrame;
	
	private JMenuBar menuBar;
	private JMenu fileMenu;
	private JMenuItem newMenuItem;
	private JMenuItem openMenuItem;
	private JMenuItem saveMenuItem;
	private JMenuItem saveAsMenuItem;
	private JMenuItem exitMenuItem;
	private JMenu editMenu;
	private JMenuItem undoMenuItem;
	private JMenuItem redoMenuItem;
	private JMenuItem copyCodeMenuItem;
	private JMenuItem duplicateMenuItem;
	private JMenuItem deleteMenuItem;
	private JMenuItem preferencesMenuItem;
	private JMenu projectMenu;
	private JMenu addMenu;
	private JMenuItem addClassMenuItem;
	private JMenuItem addAbstractClassMenuItem;
	private JMenuItem addEnumMenuItem;
	private JMenuItem addInterfaceMenuItem;
	private JMenuItem hasAMenuItem;
	private JMenuItem isAMenuItem;
	private JMenuItem unLinkMenuItem;
	private JMenuItem generateMenuItem;
	private JMenuItem generateAllMenuItem;
	private JMenu helpMenu;
	private JMenuItem contentsMenuItem;
	private JMenuItem aboutMenuItem;
	
	private JToolBar toolBar;
	private JButton newToolBarButton;
	private JButton openToolBarButton;
	private JButton saveToolBarButton;
	private JButton copyCodeToolBarButton;
	private JButton duplicateToolBarButton;
	private JButton deleteToolBarButton;
	private JButton copyToolBarButton;
	private JButton addClassToolBarButton;
	private JButton hasAToolBarButton;
	private JButton isAToolBarButton;
	private JButton unLinkToolBarButton;
	private JButton generateAllToolBarButton;
	//private JButton
	
	
	
	public MainFrame() {
		options = Options.getInstance();
		log = Log.getInstance();
		lang = Options.getLanguage();
		classes = Classes.getInstance();
		lines = Lines.getInstance();
		
		desktopPane = new DesktopPane();
		desktopPane.setSize(10000, 10000);
		JScrollPane scrollPane= new JScrollPane(desktopPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		add(scrollPane);

		//try {
			super.setTitle(lang.getString("MainFrame.text"));
			createMenuBar();
			createToolBar();
		//} catch (MissingResourceException missingResourceException) {
			
		//}
		//this.addMouseListener(new a());
	}
	
	/*
	 * Tao menunar chinh cua chuong trinh
	 */
	private void createMenuBar() {
		ResourceBundle lang = Options.getLanguage();
		Themes.Theme theme = Themes.Theme.getInstance();
		
		menuBar = new JMenuBar();
		fileMenu = new JMenu();
		newMenuItem = new JMenuItem();
		openMenuItem = new JMenuItem();
		saveMenuItem = new JMenuItem();
		saveAsMenuItem = new JMenuItem();
		exitMenuItem = new JMenuItem();
		editMenu = new JMenu();
		undoMenuItem = new JMenuItem();
		redoMenuItem = new JMenuItem();
		copyCodeMenuItem = new JMenuItem();
		duplicateMenuItem = new JMenuItem();
		deleteMenuItem = new JMenuItem();
		preferencesMenuItem = new JMenuItem();
		projectMenu = new JMenu();
		addMenu = new JMenu();
		addAbstractClassMenuItem = new JMenuItem();;
		addEnumMenuItem = new JMenuItem();
		addInterfaceMenuItem = new JMenuItem();
		hasAMenuItem = new JMenuItem();
		isAMenuItem = new JMenuItem();
		unLinkMenuItem = new JMenuItem();
		generateMenuItem = new JMenuItem();
		generateAllMenuItem = new JMenuItem();
		addClassMenuItem = new JMenuItem();
		helpMenu = new JMenu();
		contentsMenuItem = new JMenuItem();
		aboutMenuItem = new JMenuItem();		
		
		fileMenu.setText(lang.getString("MainFrame.fileMenu.text"));
		fileMenu.setMnemonic(KeyEvent.VK_F);
		menuBar.add(fileMenu);
		newMenuItem.setText(lang.getString("MainFrame.newMenuItem.text"));
		newMenuItem.setIcon(theme.getImage("newIcon"));
		newMenuItem.setMnemonic(KeyEvent.VK_N);
		newMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		newMenuItem.addActionListener(this);
		fileMenu.add(newMenuItem);
		openMenuItem.setText(lang.getString("MainFrame.openMenuItem.text"));
		openMenuItem.setIcon(theme.getImage("openIcon"));
		openMenuItem.setMnemonic(KeyEvent.VK_O);
		openMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		openMenuItem.addActionListener(this);
		fileMenu.add(openMenuItem);
		fileMenu.addSeparator();
		saveMenuItem.setText(lang.getString("MainFrame.saveMenuItem.text"));
		saveMenuItem.setIcon(theme.getImage("saveIcon"));
		saveMenuItem.setMnemonic(KeyEvent.VK_S);
		saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		saveMenuItem.addActionListener(this);
		fileMenu.add(saveMenuItem);
		saveAsMenuItem.setText(lang.getString("MainFrame.saveAsMenuItem.text"));
		saveAsMenuItem.addActionListener(this);
		fileMenu.add(saveAsMenuItem);
		fileMenu.addSeparator();
		exitMenuItem.setText(lang.getString("MainFrame.exitMenuItem.text"));
		exitMenuItem.setIcon(theme.getImage("exitIcon"));
		exitMenuItem.setMnemonic(KeyEvent.VK_E);
		exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_4, ActionEvent.ALT_MASK));
		exitMenuItem.addActionListener(this);
		fileMenu.add(exitMenuItem);
		
		editMenu.setText(lang.getString("MainFrame.editMenu.text"));
		editMenu.setMnemonic(KeyEvent.VK_E);
		menuBar.add(editMenu);
		undoMenuItem.setText(lang.getString("MainFrame.undoMenuItem.text"));
		undoMenuItem.addActionListener(this);
		undoMenuItem.setEnabled(false);
		editMenu.add(undoMenuItem);
		redoMenuItem.setText(lang.getString("MainFrame.redoMenuItem.text"));
		redoMenuItem.addActionListener(this);
		redoMenuItem.setEnabled(false);
		editMenu.add(redoMenuItem);
		editMenu.addSeparator();
		copyCodeMenuItem.setText(lang.getString("MainFrame.copyCodeMenuItem.text"));
		copyCodeMenuItem.setIcon(theme.getImage("copyCodeIcon"));
		copyCodeMenuItem.setMnemonic(KeyEvent.VK_C);
		copyCodeMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_C, ActionEvent.CTRL_MASK));
		copyCodeMenuItem.addActionListener(this);
		editMenu.add(copyCodeMenuItem);
		duplicateMenuItem.setText(lang.getString("MainFrame.duplicateMenuItem.text"));
		duplicateMenuItem.setIcon(theme.getImage("duplicateIcon"));
		duplicateMenuItem.setMnemonic(KeyEvent.VK_D);
		duplicateMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_D, ActionEvent.CTRL_MASK));
		duplicateMenuItem.addActionListener(this);
		editMenu.add(duplicateMenuItem);
		deleteMenuItem.setText(lang.getString("MainFrame.deleteMenuItem.text"));
		deleteMenuItem.setIcon(theme.getImage("deleteIcon"));
		deleteMenuItem.setMnemonic(KeyEvent.VK_E);
		deleteMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_X, ActionEvent.CTRL_MASK));
		deleteMenuItem.addActionListener(this);
		editMenu.add(deleteMenuItem);
		editMenu.addSeparator();
		preferencesMenuItem.setText(lang.getString("MainFrame.preferencesMenuItem.text"));
		preferencesMenuItem.setIcon(theme.getImage("preferencesIcon"));
		preferencesMenuItem.setMnemonic(KeyEvent.VK_P);
		preferencesMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_P, ActionEvent.CTRL_MASK));
		preferencesMenuItem.addActionListener(this);
		editMenu.add(preferencesMenuItem);
		
		projectMenu.setText(lang.getString("MainFrame.projectMenu.text"));
		projectMenu.setMnemonic(KeyEvent.VK_P);
		menuBar.add(projectMenu);
		addMenu.setText(lang.getString("MainFrame.projectMenu.addMenu.text"));
		addMenu.setMnemonic(KeyEvent.VK_A);
		addMenu.setIcon(theme.getImage("addIcon"));
		projectMenu.add(addMenu);
		addClassMenuItem.setText(lang.getString("MainFrame.addClassMenuItem.text"));
		addClassMenuItem.setMnemonic(KeyEvent.VK_L);
		addClassMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_L, ActionEvent.CTRL_MASK));
		preferencesMenuItem.addActionListener(this);
		addClassMenuItem.addActionListener(this);
		addMenu.add(addClassMenuItem);
		addAbstractClassMenuItem.setText(lang.getString("MainFrame.addAbstractClassMenuItem.text"));
		addAbstractClassMenuItem.setMnemonic(KeyEvent.VK_B);
		addAbstractClassMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_B, ActionEvent.CTRL_MASK));
		addAbstractClassMenuItem.addActionListener(this);
		addMenu.add(addAbstractClassMenuItem);
		addEnumMenuItem.setText(lang.getString("MainFrame.addEnumMenuItem.text"));
		addEnumMenuItem.setMnemonic(KeyEvent.VK_E);
		addEnumMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_E, ActionEvent.CTRL_MASK));
		addEnumMenuItem.addActionListener(this);
		addMenu.add(addEnumMenuItem);
		addInterfaceMenuItem.setText(lang.getString("MainFrame.addInterfaceMenuItem.text"));
		addInterfaceMenuItem.setMnemonic(KeyEvent.VK_I);
		addInterfaceMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_I, ActionEvent.CTRL_MASK));
		addInterfaceMenuItem.addActionListener(this);
		addMenu.add(addInterfaceMenuItem);
		projectMenu.addSeparator();
		hasAMenuItem.setText(lang.getString("MainFrame.hasAMenuItem.text"));
		hasAMenuItem.setIcon(theme.getImage("hasAIcon"));
		hasAMenuItem.setMnemonic(KeyEvent.VK_H);
		hasAMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_J, ActionEvent.CTRL_MASK));
		hasAMenuItem.addActionListener(this);
		projectMenu.add(hasAMenuItem);
		isAMenuItem.setText(lang.getString("MainFrame.isAMenuItem.text"));
		isAMenuItem.setIcon(theme.getImage("isAIcon"));
		isAMenuItem.setMnemonic(KeyEvent.VK_I);
		isAMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_M, ActionEvent.CTRL_MASK));
		isAMenuItem.addActionListener(this);
		projectMenu.add(isAMenuItem);
		unLinkMenuItem.setText(lang.getString("MainFrame.unLinkMenuItem.text"));
		unLinkMenuItem.setIcon(theme.getImage("unLinkIcon"));
		unLinkMenuItem.setMnemonic(KeyEvent.VK_K);
		unLinkMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_K, ActionEvent.CTRL_MASK));
		unLinkMenuItem.addActionListener(this);
		projectMenu.add(unLinkMenuItem);
		projectMenu.addSeparator();
		generateMenuItem.setText(lang.getString("MainFrame.generateMenuItem.text"));
		generateMenuItem.setMnemonic(KeyEvent.VK_E);
		generateMenuItem.addActionListener(this);
		projectMenu.add(generateMenuItem);
		generateAllMenuItem.setText(lang.getString("MainFrame.generateAllMenuItem.text"));
		generateAllMenuItem.setMnemonic(KeyEvent.VK_G);
		generateAllMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_G, ActionEvent.CTRL_MASK));
		generateAllMenuItem.setIcon(theme.getImage("generateAllIcon"));
		generateAllMenuItem.addActionListener(this);
		projectMenu.add(generateAllMenuItem);
		
		helpMenu.setText(lang.getString("MainFrame.helpMenu.text"));
		helpMenu.setMnemonic(KeyEvent.VK_H);
		menuBar.add(helpMenu);
		contentsMenuItem.setText(lang.getString("MainFrame.contentsMenuItem"));
		contentsMenuItem.setIcon(theme.getImage("helpIcon"));
		contentsMenuItem.setMnemonic(KeyEvent.VK_C);
		contentsMenuItem.setAccelerator(KeyStroke.getKeyStroke(
		        KeyEvent.VK_T, ActionEvent.CTRL_MASK));
		contentsMenuItem.addActionListener(this);
		helpMenu.add(contentsMenuItem);
		aboutMenuItem.setText(lang.getString("MainFrame.aboutMenuItem"));
		aboutMenuItem.setIcon(theme.getImage("aboutIcon"));
		aboutMenuItem.setMnemonic(KeyEvent.VK_A);
		aboutMenuItem.addActionListener(this);
		helpMenu.add(aboutMenuItem);
		
		

		
		setJMenuBar(menuBar);
	}
	
	/*
	 * Tao toolbar chinh cua chuong trinh
	 */
	private void createToolBar() {
		ResourceBundle lang = Options.getLanguage();
		Themes.Theme theme = Themes.Theme.getInstance();
		
		toolBar = new JToolBar("Tool bar");
		newToolBarButton = new JButton();
		openToolBarButton = new JButton();
		saveToolBarButton = new JButton();
		copyCodeToolBarButton = new JButton();
		duplicateToolBarButton = new JButton();
		deleteToolBarButton = new JButton();
		copyToolBarButton = new JButton();
		addClassToolBarButton = new JButton();
		hasAToolBarButton = new JButton();
		isAToolBarButton = new JButton();
		unLinkToolBarButton = new JButton();
		generateAllToolBarButton = new JButton();
		
		
		newToolBarButton.setBorder(null);
		openToolBarButton.setBorder(null);
		saveToolBarButton.setBorder(null);
		//undoToolBarButton.setBorder(null);
		//redoToolBarButton.setBorder(null);
		copyToolBarButton.setBorder(null);
		copyCodeToolBarButton.setBorder(null);
		duplicateToolBarButton.setBorder(null);
		deleteToolBarButton.setBorder(null);
		addClassToolBarButton.setBorder(null);
		hasAToolBarButton.setBorder(null);
		isAToolBarButton.setBorder(null);
		unLinkToolBarButton.setBorder(null);
		generateAllToolBarButton.setBorder(null);
				
		newToolBarButton.setIcon(theme.getImage("newIcon"));
		openToolBarButton.setIcon(theme.getImage("openIcon"));
		saveToolBarButton.setIcon(theme.getImage("saveIcon"));
		//undoToolBarButton.setIcon(theme.getImage("undoIcon"));
		//redoToolBarButton.setIcon(theme.getImage("redoIcon"));
		copyToolBarButton.setIcon(theme.getImage("copyIcon"));
		copyCodeToolBarButton.setIcon(theme.getImage("copyCodeIcon"));
		duplicateToolBarButton.setIcon(theme.getImage("duplicateIcon"));
		deleteToolBarButton.setIcon(theme.getImage("deleteIcon"));
		addClassToolBarButton.setIcon(theme.getImage("addIcon"));
		hasAToolBarButton.setIcon(theme.getImage("hasAIcon"));
		isAToolBarButton.setIcon(theme.getImage("isAIcon"));
		unLinkToolBarButton.setIcon(theme.getImage("unLinkIcon"));
		generateAllToolBarButton.setIcon(theme.getImage("generateAllIcon"));
		
		newToolBarButton.setToolTipText(lang.getString("MainFrame.newToolBarButton.toolTipText"));
		openToolBarButton.setToolTipText(lang.getString("MainFrame.openToolBarButton.toolTipText"));
		saveToolBarButton.setToolTipText(lang.getString("MainFrame.saveToolBarButton.toolTipText"));
		//undoToolBarButton.setToolTipText(lang.getString("MainFrame.undoToolBarButton.toolTipText"));
		//redoToolBarButton.setToolTipText(lang.getString("MainFrame.redoToolBarButton.toolTipText"));
		copyCodeToolBarButton.setToolTipText(lang.getString("MainFrame.copyCodeToolBarButton.toolTipText"));
		duplicateToolBarButton.setToolTipText(lang.getString("MainFrame.duplicateToolBarButton.toolTipText"));
		deleteToolBarButton.setToolTipText(lang.getString("MainFrame.deleteToolBarButton.toolTipText"));
		addClassToolBarButton.setToolTipText(lang.getString("MainFrame.addClassToolBarButton.toolTipText"));
		hasAToolBarButton.setToolTipText(lang.getString("MainFrame.hasAToolBarButton.toolTipText"));
		isAToolBarButton.setToolTipText(lang.getString("MainFrame.isAToolBarButton.toolTipText"));
		unLinkToolBarButton.setToolTipText(lang.getString("MainFrame.unLinkToolBarButton.toolTipText"));
		//generateToolBarButton.setToolTipText(lang.getString("MainFrame.generateToolBarButton.toolTipText"));
		generateAllToolBarButton.setToolTipText(lang.getString("MainFrame.generateAllToolBarButton.toolTipText"));		
		
		toolBar.add(newToolBarButton);
		toolBar.add(openToolBarButton);
		toolBar.add(saveToolBarButton);
		toolBar.addSeparator();
		//toolBar.add(undoToolBarButton);
		//toolBar.add(redoToolBarButton);
		toolBar.add(copyCodeToolBarButton);
		toolBar.add(duplicateToolBarButton);
		toolBar.add(deleteToolBarButton);
		toolBar.addSeparator();
		toolBar.add(addClassToolBarButton);
		toolBar.add(hasAToolBarButton);
		toolBar.add(isAToolBarButton);
		toolBar.add(unLinkToolBarButton);
		toolBar.addSeparator();
		toolBar.add(generateAllToolBarButton);
		add(toolBar, BorderLayout.PAGE_START);
		
		newToolBarButton.addActionListener(this);
		openToolBarButton.addActionListener(this);
		saveToolBarButton.addActionListener(this);
		copyCodeToolBarButton.addActionListener(this);
		duplicateToolBarButton.addActionListener(this);
		deleteToolBarButton.addActionListener(this);
		addClassToolBarButton.addActionListener(this);
		hasAToolBarButton.addActionListener(this);
		isAToolBarButton.addActionListener(this);
		unLinkToolBarButton.addActionListener(this);
		generateAllToolBarButton.addActionListener(this);
	}
	
	/*
	 * Phan tich su kien
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent event) {
		if ((this.desktopPane.getSelectedFrame()) != null) { 
			try {
				((ClassFrame)this.desktopPane.getSelectedFrame()).save();
			} catch (Exception e) {
				
			}
		}
		if (event.getSource() == newMenuItem) {
			newMenuItem_Clicked();
		}
		if (event.getSource() == openMenuItem) {
			openMenuItem_Clicked();
		}
		if (event.getSource() == saveMenuItem) {
			saveMenuItem_Clicked();
		}
		if (event.getSource() == saveAsMenuItem) {
			saveAsMenuItem_Clicked();
		}
		if (event.getSource() == exitMenuItem) {
			exitMenuItem_Clicked();
		}
		if (event.getSource() == undoMenuItem) {
			unreMenuItem_Clicked(1);
		}
		if (event.getSource() == redoMenuItem) {
			unreMenuItem_Clicked(-1);
		}
		if (event.getSource() == copyCodeMenuItem) {
			copyCodeMenuItem_Clicked();
		}
		if (event.getSource() == duplicateMenuItem) {
			duplicateMenuItem_Clicked();
		}
		if (event.getSource() == deleteMenuItem) {
			deleteMenuItem_Clicked();
		}
		if (event.getSource() == preferencesMenuItem) {
			preferencesMenuItem_Clicked();
		}
		if (event.getSource() == addClassMenuItem) {
			addClassMenuItem_Clicked();
		}
		if (event.getSource() == addAbstractClassMenuItem) {
			addAbstractClassMenuItem_Clicked();
		}
		if (event.getSource() == addEnumMenuItem) {
			addEnumMenuItem_Clicked();
		}
		if (event.getSource() == addInterfaceMenuItem) {
			addInterfaceMenuItem_Clicked();
		}
		if (event.getSource() == hasAMenuItem) {
			hasAMenuItem_Clicked();
			System.out.print("sdf");
		}
		if (event.getSource() == isAMenuItem) {
			isAMenuItem_Clicked();
		}
		if (event.getSource() == unLinkMenuItem) {
			unLinkMenuItem_Clicked();
		}
		if (event.getSource() == generateMenuItem) {
			generateMenuItem_Clicked();
		}
		if (event.getSource() == generateAllMenuItem) {
			generateAllMenuItem_Clicked();
		}
		if (event.getSource() == contentsMenuItem) {
			contentsMenuItem_Clicked();
		}
		if (event.getSource() == aboutMenuItem) {
			aboutMenuItem_Clicked();
		}
		
		if (event.getSource() == newToolBarButton) {
			newMenuItem_Clicked();
		}
		if (event.getSource() == saveToolBarButton) {
			saveMenuItem_Clicked();
		}
		if (event.getSource() == openToolBarButton) {
			openMenuItem_Clicked();
		}
		if (event.getSource() == copyCodeToolBarButton) {
			copyCodeMenuItem_Clicked();
		}
		if (event.getSource() == duplicateToolBarButton) {
			duplicateMenuItem_Clicked();
		}
		if (event.getSource() == deleteToolBarButton) {
			deleteMenuItem_Clicked();
		}
		if (event.getSource() == addClassToolBarButton) {
			addClassMenuItem_Clicked();
		}
		if (event.getSource() == hasAToolBarButton) {
			hasAMenuItem_Clicked();
		}
		if (event.getSource() == isAToolBarButton) {
			isAMenuItem_Clicked();
		}
		if (event.getSource() == unLinkToolBarButton) {
			unLinkMenuItem_Clicked();
		}
		if (event.getSource() == generateAllToolBarButton) {
			generateAllMenuItem_Clicked();
		}
		
		
	}
	
	/*
	 * Tao phime lam viec moi
	 */
	public void newMenuItem_Clicked() {
		if (!isSaved) {
			int returnVal = Message.showConfirm(lang.getString("MainFrame.message.unSavedConfirm"));
			if (returnVal == JOptionPane.YES_OPTION) {
				saveMenuItem_Clicked();
			} else if (returnVal == JOptionPane.CANCEL_OPTION) return;
		}
			log.reset();
			desktopPane.removeAll();
			desktopPane.updateUI();
			isSaved = true;
			file = null;
			classes.reset();
			lines.reset();
			desktopPane.updateUI();
	}
	
	/*
	 * Mo 1 phiem lam viec da luu trong file jcg
	 */
	public void openMenuItem_Clicked() {
		newMenuItem_Clicked();
		
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileFilter(new JCGFilter());
		
		int returnVal = fileChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
			// Mo file XML
			try {
				XML_IO io = new XML_IO();
				LinkedList<SimpleLine> sl = new LinkedList<SimpleLine>();
				LinkedList<Data.Shape> shapes = new LinkedList<Data.Shape>();
				io.open(classes.getClasses(), shapes , sl, file);
				int size = classes.size();
				for (int i = 0; i < size; i++) {
					ClassFrame t = new ClassFrame(classes.getClasses().get(i), this);
					t.setLocation(shapes.get(i).getX(), shapes.get(i).getY());
					t.setSize(shapes.get(i).getWidth(), shapes.get(i).getHeight());
					desktopPane.add(t);
					t.setId(classes.getClasses().get(i).getId());					
				}
				
				size = sl.size();
				for (int i = 0; i < size; i++) {
					Line l = new Line(classes.getClass(sl.get(i).getHead()).getClassFrame(),
							classes.getClass(sl.get(i).getTail()).getClassFrame(), sl.get(i).getType());
					lines.add(l);
					classes.getClass(sl.get(i).getTail()).addBase(classes.getClass(sl.get(i).getHead()));
				}
				
			} catch (Exception e) {
				Message.showMessage(lang.getString("MainFrame.message.openFileError"));
			}
			desktopPane.updateUI();
		}
	}
	
	/*
	 * Luu phiem lam viec hien tai
	 */
	public void saveMenuItem_Clicked() {
		JInternalFrame jif[] = getAllFrames();
		for (int i = 0; i < jif.length; i++) {
			((ClassFrame)jif[i]).save();
		}
		if (file == null) {
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.setFileFilter(new JCGFilter());
			int returnVal = fileChooser.showSaveDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				file = fileChooser.getSelectedFile();
			} else return;
		}
		try {
			XML_IO io = new XML_IO();
			// Luu thong tin vao file XML
			io.save(classes.getClasses(), lines.getLines(), file);
			
		} catch (Exception e) {
			Message.showMessage(lang.getString("MainFrame.message.saveFileError") + e.getMessage());
		}
		
	}
	
	/*
	 * Save as phien lam viec
	 */
	public void saveAsMenuItem_Clicked() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileFilter(new JCGFilter());
		
		int returnVal = fileChooser.showSaveDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
			try {
				XML_IO io = new XML_IO();
				io.save(classes.getClasses(), lines.getLines(), file);
			} catch (Exception e) {
				Message.showMessage(lang.getString("MainFrame.message.saveFileError"));
			}
		}
	}
	
	/*
	 * Su kien click exit (thoat chuong trinh) 
	 */
	public void exitMenuItem_Clicked() {
		System.exit(0);
	}
	
	/*
	 * Undo - redo
	 * Loi
	 */
	public void unreMenuItem_Clicked(int type) {
		Version v;
        if (type == 1 && log.canGetPrevious()) {
                v = log.getPrevious();
        } else if (type == -1 && log.canGetNext()) {
                v = log.getNext();
        } else return;
        classes.setClasses(v.classes);

        JInternalFrame frames[] = GUI.MainFrame.getAllFrames();
		for (int i = 0; i < frames.length; i++) {
			frames[i].setVisible(false);
		}
		for (int i = 0; i < classes.getClasses().size(); i++) {
			if (classes.getClasses().get(i).getClassFrame() != null) {
				classes.getClasses().get(i).getClassFrame().updateClass(classes.getClasses().get(i));
				classes.getClasses().get(i).getClassFrame().setVisible(true);
			} else {
				ClassFrame t = new ClassFrame(classes.getClasses().get(i), this);
				desktopPane.add(t);
				t.setId(classes.getClasses().get(i).getId());
			}
		}

	}
	
	/*
	 * Su kien copy ma nguon
	 */
	public void copyCodeMenuItem_Clicked() {
		Generate g = new Generate();
		if (desktopPane.getSelectedFrame() == null) return;
		String code = g.getCode(((ClassFrame)desktopPane.getSelectedFrame()).getClassData());
		StringSelection stringSelection = new StringSelection(code);
	    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	    clipboard.setContents(stringSelection, this);
	}
	
	/*
	 * Su kien sao chep class 
	 */
	public void duplicateMenuItem_Clicked() {
		ClassFrame t = new ClassFrame((ClassFrame)this.desktopPane.getSelectedFrame());
		Data.Class c = new Data.Class(((ClassFrame)this.desktopPane.getSelectedFrame()).getClassData());
		t.setId(c.getId());
		classes.addClass(c);
		desktopPane.add(t);
		//log.push(new ClassWithShape(null, null, t));
		//log.push(new ClassWithShape(t.classData, t));
		desktopPane.updateUI();
		MainFrame.isSaved = false;
		log.push(classes.getClasses(), lines.getLines());
		MainFrame.isLogged = true;
	}
	
	/*
	 * Su kien xoa Class
	 */
	public void deleteMenuItem_Clicked() {
		try {
		ClassFrame activedClassFrame = (ClassFrame)this.desktopPane.getSelectedFrame();
			if (this.desktopPane.getSelectedFrame() != null) {
				//log.push(new ClassWithShape(activedClassFrame.classData, activedClassFrame, null));
				classes.removeClass(activedClassFrame.getClassData());
				//desktopPane.remove(activedClassFrame);
				lines.remove(activedClassFrame);
				this.desktopPane.remove(activedClassFrame);
				//this.desktopPane.getSelectedFrame().setVisible(false);
				desktopPane.updateUI();
				MainFrame.isSaved = false;
				log.push(classes.getClasses(), lines.getLines());
				MainFrame.isLogged = true;
			}
		} catch (Exception e) {
			Message.showMessage(lang.getString("MainFrame.message.deleteError"));
		}
	}
	
	/*
	 * Hien hop thoai tuy chon
	 */
	public void preferencesMenuItem_Clicked() {
		options.show(this);
	}
	
	/*
	 * Them 1 class
	 */
	public void addClassMenuItem_Clicked() {
		ClassFrame t = new ClassFrame(Data.Class.CLASS, this);
		desktopPane.add(t);
		Data.Class classData = new Data.Class();
		classes.addClass(classData);
		t.setId(classData.getId());
		MainFrame.isSaved = false;
		log.push(classes.getClasses(), lines.getLines());
		MainFrame.isLogged = true;
		//log.push(classes.getClasses(), lines.getLines());
		//log.push(new ClassWithShape(t.classData, t));
	}
	
	/*
	 * Them 1 abstract class
	 */
	public void addAbstractClassMenuItem_Clicked() {
		ClassFrame t = new ClassFrame(Data.Class.ABSTRACT_CLASS, this);
		desktopPane.add(t);
		Data.Class classData = new Data.Class(Data.Class.ABSTRACT_CLASS);
		classes.addClass(classData);
		t.setId(classData.getId());
		MainFrame.isSaved = false;
		log.push(classes.getClasses(), lines.getLines());
		MainFrame.isLogged = true;
		//log.push(classes.getClasses(), lines.getLines());
		//log.push(new ClassWithShape(t.classData, t));
	}
	
	/*
	 * Them mot enumeration
	 */
	public void addEnumMenuItem_Clicked() {
		ClassFrame t = new ClassFrame(Data.Class.ENUM, this);
		desktopPane.add(t);
		Data.Class classData = new Data.Class(Data.Class.ENUM);
		classes.addClass(classData);
		t.setId(classData.getId());
		MainFrame.isSaved = false;
		log.push(classes.getClasses(), lines.getLines());
		MainFrame.isLogged = true;
		//log.push(classes.getClasses(), lines.getLines());
		//log.push(new ClassWithShape(t.classData, t));
	}
	
	/*
	 * Them 2 interface
	 */
	public void addInterfaceMenuItem_Clicked() {
		ClassFrame t = new ClassFrame(Data.Class.INTERFACE, this);
		desktopPane.add(t);
		Data.Class classData = new Data.Class(Data.Class.INTERFACE);
		classes.addClass(classData);
		t.setId(classData.getId());
		MainFrame.isSaved = false;
		log.push(classes.getClasses(), lines.getLines());
		MainFrame.isLogged = true;
		//log.push(classes.getClasses(), lines.getLines());
		//log.push(new ClassWithShape(t.classData, t));
	}
	
	/*
	 * Them qua he has a giua 2 class
	 */
	public void hasAMenuItem_Clicked() {
		try {
			lines.add(new Line(lastActivedClassFrame, (ClassFrame)desktopPane.getSelectedFrame(), Line.HAS_A));

			MainFrame.isSaved = false;
			log.push(classes.getClasses(), lines.getLines());
			MainFrame.isLogged = true;
			desktopPane.updateUI();
		} catch (Exception e) {
			Message.showMessage(lang.getString("MainFrame.message.hasAError"));
		}
	}
	
	/*
	 * Them qua he is a giua 2 class
	 */
	public void isAMenuItem_Clicked() {
		try {
			ClassFrame cf1 = lastActivedClassFrame;
			ClassFrame cf2 = (ClassFrame)desktopPane.getSelectedFrame();
			lines.add(new Line(lastActivedClassFrame, (ClassFrame)desktopPane.getSelectedFrame(), Line.IS_A));
			cf2.getClassData().addBase(cf1.getClassData());
			MainFrame.isSaved = false;
			log.push(classes.getClasses(), lines.getLines());
			MainFrame.isLogged = true;
			desktopPane.updateUI();
		} catch (Exception e) {
			Message.showMessage(lang.getString("MainFrame.message.isAError"));
		}
	}
	
	/*
	 * Xoa quan he giua 2 class
	 */
	public void unLinkMenuItem_Clicked() {
		try {
			lines.remove(lastActivedClassFrame, (ClassFrame)desktopPane.getSelectedFrame());
			lastActivedClassFrame.getClassData().removeBase(((ClassFrame)desktopPane.getSelectedFrame()).getClassData());
			((ClassFrame)desktopPane.getSelectedFrame()).getClassData().removeBase(lastActivedClassFrame.getClassData());
			MainFrame.isSaved = false;
			log.push(classes.getClasses(), lines.getLines());
			MainFrame.isLogged = true;
			desktopPane.updateUI();
		} catch (Exception e) {
			Message.showMessage(lang.getString("MainFrame.message.unLinkError"));
		}
	}
	
	/*
	 * Sinh ma cho selected class
	 */
	public void generateMenuItem_Clicked() {
		if (desktopPane.getSelectedFrame() == null) return;
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileFilter(new FolderFilter());
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		
		int returnVal = fileChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
			try {
				Generate g = new Generate();
				g.GenerateCode(((ClassFrame)desktopPane.getSelectedFrame()).getClassData(), file);
			} catch (Exception e) {
				Message.showMessage(lang.getString("MainFrame.message.generateFileError"));
			}
		}
	}
	
	/*
	 * Sinh ma cho tat ca cac class
	 */
	public void generateAllMenuItem_Clicked() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileFilter(new FolderFilter());
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		
		int returnVal = fileChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
			try {
				Generate g = new Generate();
				g.GenerateCode(classes.getClasses(), file);
			} catch (Exception e) {
				Message.showMessage(lang.getString("MainFrame.message.generateFileError"));
			}
		}
	}
	
	public void contentsMenuItem_Clicked() {
		Desktop desktop = Desktop.getDesktop();
		try {
			desktop.open(new File("JCG.chm"));
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, lang.getString("MainFrame.message.helpError"));
		}
	}
	
	/*
	 * Hien hop thong bao about
	 */
	public void aboutMenuItem_Clicked() {
		AboutDialog a = new AboutDialog(this);
	}

	public static JInternalFrame[] getAllFrames() {
		return desktopPane.getAllFrames();
	}
	
	public void lostOwnership( Clipboard aClipboard, Transferable aContents) {
	     //do nothing
	}
	public void setClipboardContents( String aString ){
		StringSelection stringSelection = new StringSelection( aString );
	    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	    clipboard.setContents( stringSelection, this );
	}

}
